import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_applearnprogrom/LoginView.dart';
import 'package:flutter_applearnprogrom/routers/app_router.dart';
import 'package:flutter_applearnprogrom/routers/routes.dart';
import 'package:flutter_applearnprogrom/video/Home.dart';
import 'package:flutter_applearnprogrom/viewmodel/RegisterViewmodel.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_applearnprogrom/viewmodel/theme_viewmodel.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:flutter_applearnprogrom/routers/Routesbyhander.dart';
import 'Application.dart';
import 'dart:io';

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences sp = await SharedPreferences.getInstance();
  int color = await sp.getInt("color") ?? 0;
  Theme_ViewModel themeViewmodel = Theme_ViewModel();
  themeViewmodel.setColor(color);
  runApp(MyApp());
  runApp(MultiProvider(
    providers: [
//      ChangeNotifierProvider(create: (context) => LoginViewmodel()),
      ChangeNotifierProvider(create: (context) => RegisterViewmodel()),
      ChangeNotifierProvider(create: (context) => themeViewmodel),
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
//  final JPush jpush = new JPush();\

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initPlatformState();
    Router router = Router();
    Routesbyhander.configureRoutes(router);
    Application.router = router;

    //  设置状态栏沉浸vvFlutter沉浸式状态栏
    if (Platform.isAndroid) {
      SystemUiOverlayStyle style = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,

        /// 这是设置状态栏的图标和字体的颜色
        /// Brightness.light  一般都是显示为白色
        /// Brightness.dark 一般都是显示为黑色
        statusBarIconBrightness: Brightness.light,
      );
      SystemChrome.setSystemUIOverlayStyle(style);
      initialRoute:
      AppRouter.initialRoute;
      unknownRoute:
      AppRouter.unknownRoute;
    }
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    const bool inProduction = const bool.fromEnvironment("dart.vm.product");
//    try {
//      jpush.addEventHandler(
//          onReceiveNotification: (Map<String, dynamic> message) async {
//            print("flutter onReceiveNotification: $message");
//          }, onOpenNotification: (Map<String, dynamic> message) async {
//        print("flutter onOpenNotification: $message");
//      }, onReceiveMessage: (Map<String, dynamic> message) async {
//        print("flutter onReceiveMessage: $message");
//      }, onReceiveNotificationAuthorization:
//          (Map<String, dynamic> message) async {
//        print("flutter onReceiveNotificationAuthorization: $message");
//      });
//    } on PlatformException {
//      platformVersion = 'Failed to get platform version.';
//    }
//    print("开发模式" + inProduction.toString());
//    jpush.setup(
//      appKey: "e700e2560560848cdeb0b283", //你自己应用的 AppKey
//      channel: "developer-default",
//      production: inProduction,
//      debug: inProduction, //debug日志 true=打印
//    );
//    jpush.applyPushAuthority(
//        new NotificationSettingsIOS(sound: true, alert: true, badge: true));
//
//    // Platform messages may fail, so we use a try/catch PlatformException.
//    jpush.getRegistrationID().then((rid) {
//      print("flutter get registration id : $rid");
//    });
  }

  @override
  Widget build(BuildContext context) {

    ///设置像素密度屏幕适配
    return MaterialApp(
        title: '国祯E健康',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(),
        home: MyApp2()
    );
  }
}

class MyApp2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    routes: routes;


    return Home();
  }
}

